package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.domain.User;
import com.service.CarService;

public class addCatServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public addCatServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String goods_id = request.getParameter("goods_id");//商品ID
		
		//业务方法
		CarService carService = new CarService();
		HttpSession session = request.getSession();
		User MyUser = (User) session.getAttribute("MyUser");
		//判断用户是否为空 为空跳转登录界面
		if (MyUser==null) {
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			return;
		}
		Boolean isAddCar = carService.addGoods(MyUser.getUsername(), goods_id);//传递给添加购物车 业务方法
		
		if (isAddCar) {
			response.sendRedirect(request.getContextPath()+"/goods-details.jsp?goods_id="+goods_id);
		} else {
			response.getWriter().write("AddCarErr");
		}
		
		//request.getRequestDispatcher("/goods-details.jsp?goods_id="+goods_id).forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
